package com.ruoyi.statistics.service.impl;

import com.ruoyi.statistics.mapper.SysStatisticsMapper;
import com.ruoyi.statistics.service.ISysStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
@Service
public class SysStatisticsServiceImpl implements ISysStatisticsService {

    @Autowired
    private SysStatisticsMapper statisticsMapper;

    @Override
    public Integer getAllStoreCount() {
        return statisticsMapper.getAllStoreCount();
    }

    @Override
    public Integer getAllAgentCount() {
        return statisticsMapper.getAllAgentCount();
    }

    @Override
    public BigDecimal getAllAmount() {
        BigDecimal allStoreAmount = statisticsMapper.getAllStoreAmount();
        BigDecimal allAgentAmount = statisticsMapper.getAllAgentAmount();
        if(allStoreAmount!=null && allAgentAmount!=null){
            return allStoreAmount.add(allAgentAmount);
        }

        if(allStoreAmount==null){
            return allAgentAmount;
        }

        if(allAgentAmount==null){
            return allStoreAmount;
        }

        return BigDecimal.valueOf(0);
    }

    @Override
    public Integer getAllOrder() {
        return statisticsMapper.getAllOrder();
    }

    @Override
    public Integer getAgentStoreCount(Long deptId) {
        return statisticsMapper.getAgentStoreCount(deptId);
    }

    @Override
    public Integer getAgentOrder(Long deptId) {
        return statisticsMapper.getAgentOrder(deptId);
    }

    @Override
    public BigDecimal getAgentAmount(Long deptId) {
        BigDecimal agentAmount = statisticsMapper.getAgentAmount(deptId);
        if (agentAmount==null){
            agentAmount = BigDecimal.valueOf(0);
        }
        return agentAmount;
    }

    @Override
    public Integer getAllStoreCountChange() {
        return statisticsMapper.getNowAllStoreCount() - statisticsMapper.getYesterdayAllStore();
    }

    @Override
    public Integer getAllAgentCountChange() {
        return statisticsMapper.getNowAllAgentCount() - statisticsMapper.getYesterdayAllAgentCount();
    }

    @Override
    public Integer getAllOrderChange() {
        return statisticsMapper.getNowAllOrder() - statisticsMapper.getYesterdayAllOrder();
    }

    @Override
    public BigDecimal getAllAmountChange() {
        BigDecimal nowAllStoreAmount = statisticsMapper.getNowAllStoreAmount();
        BigDecimal nowAllAgentAmount = statisticsMapper.getNowAllAgentAmount();
        BigDecimal nowAmount = null;
        BigDecimal yesterdayAmount = null;
        if(nowAllStoreAmount!=null && nowAllAgentAmount!=null){
            nowAmount = nowAllStoreAmount.add(nowAllAgentAmount);
        }else if(nowAllStoreAmount==null){
            nowAmount = nowAllAgentAmount;
        }else if(nowAllAgentAmount==null){
            nowAmount = nowAllStoreAmount;
        }else {
            nowAmount = null;
        }

        BigDecimal AllYesterdayAgentAmount = statisticsMapper.getAllYesterdayAgentAmount();
        BigDecimal AllYesterdayStoreAmount = statisticsMapper.getAllYesterdayStoreAmount();
        if(AllYesterdayAgentAmount!=null && AllYesterdayStoreAmount!=null){
            yesterdayAmount = AllYesterdayAgentAmount.add(AllYesterdayStoreAmount);
        }else if(AllYesterdayAgentAmount==null){
            yesterdayAmount = AllYesterdayStoreAmount;
        }else if(AllYesterdayStoreAmount==null){
            yesterdayAmount = AllYesterdayAgentAmount;
        }else {
            yesterdayAmount = null;
        }


        if(nowAmount!=null && yesterdayAmount!=null){
            return nowAmount.subtract(yesterdayAmount);
        }

        if(nowAmount==null&&yesterdayAmount!=null){
            return yesterdayAmount;
        }

        if(yesterdayAmount==null&&nowAmount!=null){
            return nowAmount;
        }
        return BigDecimal.valueOf(0);

    }

    @Override
    public Integer getAgentStoreCountChange(Long deptId) {
        return statisticsMapper.getNowAgentStoreCount(deptId) - statisticsMapper.getYesterdayAgentStoreCount(deptId);
    }

    @Override
    public Integer getAgentOrderChange(Long deptId) {
        return statisticsMapper.getNowAgentOrder(deptId) - statisticsMapper.getYesterdayAgentOrder(deptId);
    }

    @Override
    public BigDecimal getAgentAmountChange(Long deptId) {
        BigDecimal YesterdayAgentAmount = statisticsMapper.getYesterdayAgentAmount(deptId);
        BigDecimal NowAgentAmount = statisticsMapper.getNowAgentAmount(deptId);
        if(YesterdayAgentAmount!=null && NowAgentAmount!=null){
            return NowAgentAmount.subtract(YesterdayAgentAmount);
        }

        if(YesterdayAgentAmount==null&& NowAgentAmount!=null){
            return NowAgentAmount;
        }

        if(NowAgentAmount==null&& YesterdayAgentAmount!=null){
            return YesterdayAgentAmount;
        }

        return BigDecimal.valueOf(0);
    }
}
